package com.yonyou.ucf.mdf.app.isvdxq.other.service;

import com.yonyou.ucf.mdf.app.isvdxq.firstpart.appconfig.entity.AppconfigEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.SuiteauthEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.SuiteauthMapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SuiteauthService {
    @Autowired
    SuiteauthMapperImpl suiteauthMapper;

    public SuiteauthEntity SelectById(int id) {
        return suiteauthMapper.selectById(id);
    }

    public Boolean CheckEntity(int Id) {
        int rInt = suiteauthMapper.selectCount(Id);
        if (rInt > 0) return true;
        else return false;
    }

    public SuiteauthEntity CheckEntityByKey(String tenantId, String appkey) {
        return suiteauthMapper.selectCountByKey(tenantId, appkey);
    }

    public SuiteauthEntity CheckEntityByToken(String access_token) {
        return suiteauthMapper.selectByToken(access_token);
    }

    public SuiteauthEntity CheckEntityByToken(String access_token, boolean self) {
        if (self)
            return suiteauthMapper.selectByCusToken(access_token);
        else
            return suiteauthMapper.selectByToken(access_token);
    }

    public List<SuiteauthEntity> SelectByTenantId(String Id) {
        return suiteauthMapper.selectByTenantId(Id);
    }

    public List<SuiteauthEntity> SelectByTenantIds(List<String> Ids) {
        return suiteauthMapper.selectByTenantIds(Ids);
    }

    public List<SuiteauthEntity> SelectAll() {
        return suiteauthMapper.selectAll();
    }

    /**
     * 写入租户授权 suiteauth
     * @param entity
     * @return
     */
    public Integer InsertEntity(SuiteauthEntity entity) {
        return suiteauthMapper.insert(entity);
    }

    /**
     * 批量应用授权 appconfig
     * @param list
     * @return
     */
    public Integer InsertBatch(List<AppconfigEntity> list) {
        if (list.size() == 0) return 0;
        long start = System.currentTimeMillis();
        int res = suiteauthMapper.insertBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("---------------" + (start - end) + "---------------");
        return res;
    }

    public AppconfigEntity SelectLastEntity(String tenantId, String appCode, Integer appType) {
        return suiteauthMapper.SelectLastEntity(tenantId, appCode, appType);
    }

    public Integer UpdateEntity(SuiteauthEntity entity) {
        return suiteauthMapper.updateByPrimaryKey(entity);
    }
    public Integer UpdateToken(String tenantId, String yhtUserId, String accessToken, String expireAt) {
        return suiteauthMapper.updateByTenantId(accessToken, expireAt, yhtUserId, tenantId);
    }

    public Integer DeleteById(int id) {
        return suiteauthMapper.deleteByPrimaryKey(id);
    }

    public Integer EditrefreshTime(Integer rt, String tenantId) {
        return suiteauthMapper.EditrefreshTime(rt, tenantId);
    }
}
